//
//  SHRWDSHViewController.m
//  VmiSJ
//
//  Created by 汪的一声就哭了！ on 2017/1/6.
//  Copyright © 2017年 汪的一声就哭了！. All rights reserved.
//
//容器标题高度
#define TitleHeight self.fontSize*3
#import "SHRWDSHViewController.h"

@interface SHRWDSHViewController ()<UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation SHRWDSHViewController
{
    NSMutableArray *_listArray;
    NSInteger page;
}
- (void)setState:(NSString *)state
{
    _state = state;
    [self obtainTaskDatawith:NO];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self obtainTaskDatawith:YES];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 4, WIDTH, HEIGHT-64-TitleHeight) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"SHRWDSHTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell1"];
    [self.tableView registerNib:[UINib nibWithNibName:@"SHRWNoImgTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell2"];
    [self addRefresh];
}
- (void)addRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(obtain)];
    
    // 自动改变透明度
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopics)];
}
- (void)timerAction:(NSTimer *)timer
{
    [self.tableView numberOfRowsInSection:0];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[indexPath.row];
    if (model.images.count == 0) {
        SHRWNoImgTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        cell.userLabel.text = [NSString stringWithFormat:@"用户：%@",model.user_phone];
        cell.detailLabel.text = [model.t_message componentsJoinedByString:@"\n"];
        cell.timeLabel.text = model.submit_time;
        cell.backgroundColor = [UIColor clearColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.tongguoBtn.tag = 100+indexPath.row;
        [cell.tongguoBtn addTarget:self action:@selector(tongguoAction:) forControlEvents:UIControlEventTouchUpInside];
        cell.jujueBtn.tag = 1000+indexPath.row;
        [cell.jujueBtn addTarget:self action:@selector(jujueAction:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }
    SHRWDSHTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    cell.collectionView.tag = 100+indexPath.row;
    cell.collectionView.delegate = self;
    cell.collectionView.dataSource = self;
    [cell.collectionView reloadData];
    [cell.collectionView registerNib:[UINib nibWithNibName:@"SHRWDSHCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"collection"];
    cell.flowLayout.itemSize = CGSizeMake(70, 70);
    cell.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    cell.flowLayout.minimumInteritemSpacing = 8;
    cell.flowLayout.sectionInset = UIEdgeInsetsMake(0, 5, 0, 5);
    
    cell.userLabel.text = [NSString stringWithFormat:@"用户：%@",model.user_phone];
    cell.detailLabel.text = [model.t_message componentsJoinedByString:@"\n"];
    cell.timeLabel.text = model.submit_time;
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    cell.tongguoBtn.tag = 100+indexPath.row;
    [cell.tongguoBtn addTarget:self action:@selector(tongguoAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.jujueBtn.tag = 1000+indexPath.row;
    [cell.jujueBtn addTarget:self action:@selector(jujueAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[indexPath.row];
    NSString *str = [model.t_message componentsJoinedByString:@"\n"];
    CGRect rect = [str boundingRectWithSize:CGSizeMake(WIDTH-98, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    CGFloat height = rect.size.height;
    if (rect.size.height < 18) {
        height = 18;
    }
    if (model.images.count == 0) {
        return 84+height;
    }
    return 167+height;
}
#pragma mark -------------- collectionView
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWDSHCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collection" forIndexPath:indexPath];
    SHRWModel *model = _listArray[collectionView.tag-100];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.images[indexPath.item]] placeholderImage:[UIImage imageNamed:@"占位图"]];
    return cell;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    SHRWModel *model = _listArray[collectionView.tag-100];
    return model.images.count;
}
#pragma mark -------------- 点击图片放大
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    SHRWModel *model = _listArray[collectionView.tag-100];
    [self.delegate SHRWDSHViewControllerDidSelectWithInt:indexPath.row andImgArray:model.images];
}
- (void)obtain {
    [self obtainTaskDatawith:NO];
}
#pragma mark -------------- 待审核任务
- (void)obtainTaskDatawith:(BOOL)hud
{
    page = 1;
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewcheck",@"type":@"1",@"task_id":self.task_id,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:hud block:^(id responseObject) {
        [self.tableView.mj_header endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            _listArray = [NSMutableArray array];
            for (NSDictionary *dic in responseObject[@"result"]) {
                SHRWModel *model = [[SHRWModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        [self.tableView reloadData];
        page++;
        [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }];
}
- (void)loadMoreTopics
{
    [NetRequiest handleDataWithString:[NSString stringWithFormat:@"%@",HTTPLOAD] dictionary:@{@"v1":@"reviewcheck",@"type":@"1",@"task_id":self.task_id,@"page":[NSString stringWithFormat:@"%ld",page]} HUD:NO block:^(id responseObject) {
        [self.tableView.mj_footer endRefreshing];
        if ([responseObject[@"code"] isEqual:@200]) {
            for (NSDictionary *dic in responseObject[@"result"]) {
                SHRWModel *model = [[SHRWModel alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_listArray addObject:model];
            }
        }
        page++;
        [self.tableView reloadData];
    }];
}
#pragma mark ----------------- 通过拒绝
- (void)tongguoAction:(UIButton *)btn
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否确定通过" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        SHRWModel *model = _listArray[btn.tag-100];
        [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"through",@"cy_id":model.cy_id} HUD:YES block:^(id responseObject) {
            [self obtainTaskDatawith:NO];
            if ([responseObject[@"code"] isEqual:@200]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)jujueAction:(UIButton *)btn
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示信息" message:@"是否确定拒绝" preferredStyle:UIAlertControllerStyleAlert];
    [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
        textField.placeholder = @"如果拒绝请输入原因";
    }];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        SHRWModel *model = _listArray[btn.tag-1000];
        if ([alert.textFields.firstObject.text isEqualToString:@""]) {
            [[SQShowMessage shareMessage] showMessageWithStr:@"请输入拒绝原因"];
        } else {
        [NetRequiest handleDataWithString:HTTPLOAD dictionary:@{@"v1":@"refuse",@"cy_id":model.cy_id,@"yuanyin":alert.textFields.firstObject.text} HUD:YES block:^(id responseObject) {
            [self obtainTaskDatawith:NO];
            if ([responseObject[@"code"] isEqual:@200]) {
                [self.navigationController popViewControllerAnimated:YES];
            }
            [[SQShowMessage shareMessage] showMessageWithStr:responseObject[@"msg"]];
        }];
        }
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
